package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.ChatRecordAdapter;
import com.example.capsule_chat0322.bean.ChatRecordBean;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.ChatRecordItemOnClickInterface;
import com.example.capsule_chat0322.request.MessagesRequest;
import com.example.capsule_chat0322.utils.AlterUtils;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/15 23:09
 */
public class SearchRecordActivity extends AppCompatActivity {

    private List<ChatRecordBean> chatRecordBeanList;
    private RecyclerView chatRecordRecyclerView;
    private ChatRecordAdapter chatRecordAdapter;
    private String roomName;
    private TextView search_record;
    private EditText search_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_chat_record);
        initView();//初始化UI控件
    }

    private void initView() {

        chatRecordRecyclerView = findViewById(R.id.chat_record_recycler_view);
        search_record = findViewById(R.id.search_record);
        search_text = findViewById(R.id.search_text);
        roomName = getIntent().getStringExtra("roomName");
        if (roomName == null) {
            return;
        }

        search_record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = search_text.getText().toString();

                if ("".equals(content)) {
                    AlterUtils.alertByToastInMainThread(SearchRecordActivity.this, "请输入搜索条件");
                    return;
                }

                doSearch(content);
            }
        });


    }

    private void doSearch(String content) {
        MessageBean messageBean = new MessageBean();
        UserInfoBean userInfoBean = (UserInfoBean) GlobalAssistMap.getObject("userInfo");
        messageBean.setField(GlobalAssistMap.getObject("field").toString());
        messageBean.setSenderName(userInfoBean.getNickname());
        messageBean.setRoomName(roomName);
        messageBean.setContent(content);
        MessagesRequest.searchChatRecord(messageBean, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                List<ChatRecordBean> chatRecordBeans = (List<ChatRecordBean>) message.obj;
                chatRecordBeanList = chatRecordBeans;
                LinearLayoutManager layoutManager = new LinearLayoutManager(SearchRecordActivity.this);
                chatRecordRecyclerView.setLayoutManager(layoutManager);
                chatRecordAdapter = new ChatRecordAdapter(chatRecordBeanList);
                adapterAddListener(chatRecordAdapter);
                chatRecordRecyclerView.setAdapter(chatRecordAdapter);
                return true;
            }
        }));

    }


    public void adapterAddListener(ChatRecordAdapter chatRecordAdapter) {

        if (chatRecordAdapter == null) {
            return;
        }

        chatRecordAdapter.setFunctionItemOnClickInterface(new ChatRecordItemOnClickInterface() {

            @Override
            public void onItemClick(RecyclerView parent, View view,String roomName, String senderId,String senderName,String content,String sendTime) {
                UserInfoBean userInfoBean = (UserInfoBean) GlobalAssistMap.getObject("userInfo");
                if(String.valueOf(userInfoBean.getId()).equals(senderId)){
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("roomName",roomName);
                intent.putExtra("senderId",senderId);
                intent.putExtra("senderName",senderName);
                intent.putExtra("content",content);
                String sendTimeStr = sendTime.contains(".") ? sendTime.substring(0, sendTime.lastIndexOf(".")) : sendTime;
                intent.putExtra("sendTime",sendTimeStr);
                intent.setClass(SearchRecordActivity.this,OneChatRecordActivity.class);
                startActivity(intent);
            }
        });
    }


}
